package com.gusha.controller;

import java.math.BigInteger;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.gusha.config.SysOperateLog;
import com.gusha.dao.SysProcessingDao;
import com.gusha.model.SysProcessing;
import com.gusha.model.SysProducts;
import com.gusha.service.SysProcessingService;
import com.gusha.utils.Constants;
import com.gusha.utils.FileUtils;
import com.gusha.utils.JsonResult;
import com.gusha.utils.WebValueUtil;



@Controller
@RequestMapping("/gx/sysProcessing")
public class SysProcessingController {
	@Autowired
	private SysProcessingDao sysProcessingDao;
	@Autowired
	private SysProcessingService sysProcessingService;
	
	
	@RequestMapping("list")
    @SysOperateLog(operate = "查看加工商城列表")
	public String list(Model model, HttpServletRequest request) {
        //请求参数
        String pageIndex = WebValueUtil.getWebValue(request, "pageIndex");
        String processingName = WebValueUtil.getWebValue(request, "processingName");
        int pageNo = 0;
        if (StringUtils.isNotBlank(pageIndex)) {
            pageNo = Integer.parseInt(pageIndex);
        }
        
        SysProcessing sysProcessing=new SysProcessing();
        if(StringUtils.isNotBlank(processingName)) {
        sysProcessing.setProcessingName(processingName);
        }
        Sort sort = new Sort(Sort.Direction.DESC, "addTime");
        Pageable pageable = PageRequest.of(pageNo, Constants.DEFAULT_PAGESIZE, sort);
        Page<SysProcessing> page = sysProcessingService.findAll(pageable, sysProcessing);
        model.addAttribute("page", page);

        return "/admin/sysProcessing/list";
		
	}
	
	@RequestMapping("toAdd")
    @SysOperateLog(operate = "进入商城添加页面")
	public String toAdd() {
		return "/admin/sysProcessing/form";
		
	}
	
    @RequestMapping("save")
    @ResponseBody
    @SysOperateLog(operate = "保存产品信息")
    public JsonResult save(SysProcessing sysProcessing,@RequestParam(name="file1") MultipartFile [] files,MultipartHttpServletRequest request) {
        JsonResult result = new JsonResult(false,"保存失败");
    	String n="";//将所有图片拼接到一个字符串里
		for (MultipartFile file : files) {
			String req= file.getOriginalFilename();//获取图片名称
		
			String imagePath = Constants.getFilePath("info")+req;//设置路径+名称
			String localPath = Constants.IMAGE_LOCAL_PATH+Constants.getFilePath("info");//设置物理地址
			FileUtils.upload(file, localPath, req);
			n=n+"#"+imagePath;
			
		}
		sysProcessing.setProcessingImg(n);
		sysProcessing.setTradeNum(new BigInteger("0"));
		sysProcessingDao.saveAndFlush(sysProcessing);
        result.setFlag(true);
        result.setMsg("保存成功");
        return result;
    }
    

	
    // 进入编辑页面
    @RequestMapping("toEdit")
    @SysOperateLog(operate = "进入产品编辑页面")
    public String toEdit(Model model, HttpServletRequest request) {

        //请求参数
        String id = WebValueUtil.getWebValue(request, "id");

        if (StringUtils.isNotBlank(id)) {
            if(sysProcessingDao.existsById(new BigInteger(id))) {
            	SysProcessing sysProcessing = sysProcessingDao.getOne(new BigInteger(id));
                model.addAttribute("obj", sysProcessing);
            }
        }        
        return "/admin/sysProcessing/form";
    }
    
    @RequestMapping("delete")
    @ResponseBody
    @SysOperateLog(operate = "删除资讯信息")
    public JsonResult delete(BigInteger id) {
        JsonResult result = new JsonResult(false,"删除失败");
        if (sysProcessingDao.existsById(id)) {
        	SysProcessing sysProcessing = sysProcessingDao.getOne(id);
        	sysProcessing.setIsDelete(new BigInteger("1"));
        	sysProcessingDao.saveAndFlush(sysProcessing);
            result.setFlag(true);
            result.setMsg("删除成功");
        }else{
            result.setFlag(false);
            result.setMsg("该对象信息不存在");
            return result;
        }
        return result;
    }


    // 恢复
    @RequestMapping("recovery")
    @ResponseBody
    @SysOperateLog(operate = "恢复资讯信息")
    public JsonResult recovery(BigInteger id) {
    	 JsonResult result = new JsonResult(false,"删除失败");
         if (sysProcessingDao.existsById(id)) {
         	SysProcessing sysProcessing = sysProcessingDao.getOne(id);
         	sysProcessing.setIsDelete(new BigInteger("0"));
         	sysProcessingDao.saveAndFlush(sysProcessing);
             result.setFlag(true);
             result.setMsg("删除成功");
         }else{
             result.setFlag(false);
             result.setMsg("该对象信息不存在");
             return result;
         }
         return result;
    }
}
